package aole.mindfx.model;

import javafx.scene.Node;
import javafx.scene.control.Button;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;

public class MindFXParam extends MindFXNode {
	private String			type;
	private MindFXMethod	parent;
	private int				pointers;

	public MindFXParam( String n, MindFXMethod m ) {
		super( n );
		parent = m;
		type = "int";
		pointers = 0;
	}

	public String toString() {
		String s = getType();
		for( int i = 0; i < getPointers(); i++ )
			s += "*";
		s += " " + getName();
		return s;
	}

	public int getPointers() {
		return pointers;
	}

	public void setPointers( int pointers ) {
		this.pointers = pointers;
	}

	public String getType() {
		return type;
	}

	public void setType( String type ) {
		this.type = type;
	}

	@Override
	public boolean setXMLAttribute( String att, String value ) {
		if( super.setXMLAttribute( att, value ) ) return true;

		switch( att ) {
		case "type":
			setType( value );
			return true;
		case "pointers":
			setPointers( Integer.parseInt( value ) );
			return true;
		}
		return false;
	}

	public MindFXMethod getFXMethod() {
		return parent;
	}

	@Override
	public Node getFXNode() {
		return null;
	}

	@Override
	public void saveXML( Document doc, Element e ) {
		e.setAttribute( "type", getType() );
		e.setAttribute( "pointers", "" + pointers );
	}

	@Override
	public void loadXML( org.w3c.dom.Node node ) {
		// get attributes
		NamedNodeMap atts = node.getAttributes();
		for( int i = 0; i < atts.getLength(); i++ ) {
			org.w3c.dom.Node att = atts.item( i );
			if( att.getNodeType() == org.w3c.dom.Node.ATTRIBUTE_NODE ) {
				if( !setXMLAttribute( att.getNodeName(), att.getNodeValue() ) )
					System.out.println( "Attribute not found: "
							+ att.getNodeName() + "=" + att.getNodeValue()
							+ "@" + getName() );
			}
		}
	}

	@Override
	public Button getCodeButton() {
		return null;
	}

}
